package com.ihunanren.biz.common;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Strings.isNullOrEmpty;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * @author kalman03
 */
public class SpringBeanLoader implements ApplicationContextAware {

	private static ApplicationContext context;
	
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		SpringBeanLoader.context = applicationContext;
		
	}

	/**
	 * 获取和spring bean名称对象的bean实例
	 * @param beanName
	 * @return
	 */
    public static Object getSpringBean(String beanName) {
    	checkArgument(!isNullOrEmpty(beanName),"bean name is required");
        checkState(context != null, "spring application context is not injected");

        return context.getBean(beanName);
    }
}

